<?php

// +----------------------------------------------------------------------
// |权限组权限服务层
// +----------------------------------------------------------------------

namespace app\admin\system;

use think\facade\Cache;
use app\admin\Model\AuthGroupsModel;
use think\facade\Db;

/**
 * 权限组
 */
class AuthGroupsService
{

      /**
     * 获取的权限分组的数据
     * @param array $param 前台传过来的参数  
     * @return array
     */
    public static function   getlist($param){
       
        $AuthGroups = new AuthGroupsModel(); 
        $pk = $AuthGroups->getPk();  
        $params= $param['params'];
        $field = $pk . ',title,status';   
        $where = [];  
        if($params['title']){
            $where[] = ['title', 'like', '%'. $params['title']. '%'];
        }       
        if($params['status']==2){
            $where[] = ['status', '=', 0];
        }else  if($params['status']==1){
            $where[] = ['status', '=', 1];
        }   
        $authgroups_list = $AuthGroups->field($field)->where($where)->order('id asc,status asc')->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray();     
        $data['list']=$authgroups_list['data'];
        $data['total']=$authgroups_list['total'];
        return $data; 
    }
         /**
     * 修改权限组或者新增权限组
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function create($param){
        $AuthGroups = new AuthGroupsModel();
        $pk = $AuthGroups->getPk();
        $params= $param['params'];    
        $data=[
            'title' => $params['title'],
            'status' => $params['status'],     
        ];

        if($params[$pk]){
             //有值则说明是修改     
             $result =  $AuthGroups::find($params[$pk])->save($data);
        }else{
            $data['createtime']=datetime();
            $data['createymd']=(int)date('Ymd',time());
             //执行添加数据
             $result =  $AuthGroups->save($data);
        }
        if($result){
            return true;
        }else{
            error('修改失败');
        }      
    }   
    /**
     * 修改权限组状态
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function update_status($param){
        $AuthGroups = new AuthGroupsModel();
        $params= $param['params'];
        if($params['status']==0 && Db::name('auth_group_access')->where('group_id',$params['id'])->find()){
            error('该权限组下存在用户，不能取消');
        }
        $result =  $AuthGroups->where('id',$params['id'])->update(['status'=>$params['status']]);
        if($result){
            return true;
        }else{
            error('修改失败');   
         }
    }
    /**
     * 删除权限组
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function delete($param){
        $AuthGroups = new AuthGroupsModel();
        $params= $param['params'];
        if( Db::name('auth_group_access')->where('group_id',$params['id'])->find()){
            error('该权限组下存在用户，不能删除');
        }
        $result =  $AuthGroups->where('id',$params['id'])->delete();
    
        if($result){
            return true;
        }else{
            error('删除失败');   
         }
      
    }

   /**
     * 权限展示
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function   purview($param){
        $AuthGroups = new AuthGroupsModel();
        $params= $param['params'];    
        $pk = $AuthGroups->getPk();
        $result =[];
        $result =  $AuthGroups->where('id',$params['id'])->field('rules')->find();    
    
        if($result['rules']) {
            $result = explode(',', $result['rules']);
        }        
        return $result;
    }

      /**
     * 权限添加
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function create_purview($param){
        $AuthGroups = new AuthGroupsModel();
        $params= $param['params'];    
        $data['rules']= implode(',', $params['rules']);
        $result = $AuthGroups->where('id',$params['id'])->update($data);
        if($result){
            return true;
        }else{
            error('修改失败');   
         }
    }
   
    
}



